#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    bool vis[101][101] = { 0 };
    int dx[4] = { 0,0,-1,1 };
    int dy[4] = { -1,1,0,0 };
    int m, n;

    bool dfs(vector<string>& board, int x, int y, string& word, int pos)
    {
        if (word.size() - 1 == pos)
        {
            return true;
        }
        vis[x][y] = true;
        for (int i = 0; i < 4; i++)
        {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a >= 0 && a < n && b >= 0 && b < m && word[pos + 1] == board[a][b] && !vis[a][b])
            {
                if (dfs(board, a, b, word, pos + 1)) return true;
            }
        }
        vis[x][y] = false;
        return false;
    }

    bool exist(vector<string>& board, string word)
    {
        n = board.size();
        m = board[0].size();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (board[i][j] == word[0])
                {
                    if (dfs(board, i, j, word, 0)) return true;
                }
            }
        }
        return false;
    }
};
int main()
{
    vector<string> v{ "a" };
    cout << Solution().exist(v, "ab");
    return 0;
}